local qiyao = fk.CreateSkill {
  name = "th_qiyao",
}

qiyao:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic,trick|.",
  mute_card = false,
  prompt = "#th_qiyao",
  interaction = function(self, player)
    local blackcards = {}
    local redcards = {}
    for _, cid in ipairs(player:getCardIds("h")) do
      if Fk:getCardById(cid).color == Card.Red then
        table.insert(redcards, cid)
      elseif Fk:getCardById(cid).color == Card.Black then
        table.insert(blackcards, cid)
      end
    end
    local names, all_names = {} , {}
    local mark = player:getTableMark("th_qiyao-turn")
    if #blackcards == 1 then
      local cardnames = Fk:getAllCardNames("t")
      table.insertTableIfNeed(all_names, cardnames)
      table.insertTableIfNeed(names, player:getViewAsCardNames(qiyao.name, cardnames, blackcards, mark))
    end
    if #redcards == 1 then
      local cardnames = Fk:getAllCardNames("b")
      table.insertTableIfNeed(all_names, cardnames)
      table.insertTableIfNeed(names, player:getViewAsCardNames(qiyao.name, cardnames, redcards, mark))
    end
    if #names == 0 then return end
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    local blackcards = {}
    local redcards = {}
    for _, cid in ipairs(player:getCardIds("h")) do
      if Fk:getCardById(cid).color == Card.Red then
        table.insert(redcards, cid)
      elseif Fk:getCardById(cid).color == Card.Black then
        table.insert(blackcards, cid)
      end
    end
    local card = Fk:cloneCard(self.interaction.data)
    if card:isCommonTrick() then
      if #blackcards ~= 1 then return end
      card:addSubcard(blackcards[1])
    else
      if #redcards ~= 1 then return end
      card:addSubcard(redcards[1])
    end
    card.skillName = qiyao.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:addTableMark(player, "th_qiyao-turn", use.card.trueName)
    if player:usedSkillTimes(qiyao.name) > 1 then
      if room:askToSkillInvoke(player, {
        skill_name = qiyao.name,
        prompt = "#th_qiyao-lost"
      }) then
        room:loseHp(player, 1, qiyao.name)
      else
        room:invalidateSkill(player, qiyao.name, "-turn", qiyao.name)
      end
    end
    if player.dead then
      return ""
    end
  end,
  enabled_at_play = function(self, player)
    return table.every(Fk:currentRoom().alive_players, function(p) return not p.dying end) and not player:isKongcheng()
  end,
  enabled_at_response = function(self, player, response)
    return table.every(Fk:currentRoom().alive_players, function(p) return not p.dying end) and not response and Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):matchExp(self.pattern) and
      not player:isKongcheng()
  end,
})

Fk:loadTranslationTable{
  ["th_qiyao"] = "七曜",
  [":th_qiyao"] = "若没有角色处于濒死状态，你可以将唯一红色手牌当基本牌、唯一黑色手牌当普通锦囊牌使用（每回合每种牌名限一次），若不为本回合首次发动，你须失去1点体力或令本回合此技能失效。",

  ["#th_qiyao"] = "你可以将唯一一张红色手牌当基本牌，将唯一一张黑色手牌当普通锦囊牌使用（每回合每种牌名限一次）",

  ["#th_qiyao-lost"] = "七曜：你须失去1点体力，否则本回合此技能失效。",
}

return qiyao